import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/user_friend/user_friend_list_model.dart';
import 'package:yysk_app/model/user_friend/user_friend_reward_list_model.dart';
import 'package:yysk_app/net/api/mall_url.dart';
import 'package:yysk_app/net/api/user_api.dart';
import 'package:yysk_app/net/http.dart';

class SearchPageController extends GetxController {
  late RxString labelName = '搜索'.obs;
  final hotWordsList = RxList([]);
  final ScrollController scrollController = ScrollController();
  final level = RxString('1');
  final total = RxInt(0);
  int pageNo = 1;
  int pageSize = 20;
  final friendList = RxList<UserFriendListItemModel>();
  final keyword = RxString('');
  final showClose = false.obs;
  final type = ''.obs;
  final friendRewardList = RxList<UserFriendRewardItemModel>();

  @override
  void onInit() {
    super.onInit();
    final params = Get.parameters ?? {};
    final args = Get.arguments ?? {};
    type.value = params['type'] ?? args['type'] ?? '';
    level.value = params['level'] ?? args['level'] ?? '1';
    // 热门搜索
    // getHotWords();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        if (type.value == 'reward') {
          getPageRewardList(loadMore: true);
        } else {
          getFriendListPage(loadMore: true);
        }
      }
    });
  }

  getList({params}) {
    if (type.value == 'reward') {
      getPageRewardList();
    } else {
      getFriendListPage();
    }
  }

  getPageRewardList({loadMore = false}) {
    if (loadMore) {
      pageNo++;
    } else {
      pageNo = 1;
    }

    try {
      http.netFetch<UserFriendRewardListModel>(
        UserApi.getPageReward,
        params: {
          'pageNo': pageNo,
          'pageSize': 20,
          'level': level.value,
          'showId': keyword.value,
        },
        success: (result) {
          List<UserFriendRewardItemModel> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNo--;
          }
          if (!loadMore) {
            friendRewardList.clear();
          }
          friendRewardList.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNo--;
      }
    }
  }

  getFriendListPage({loadMore = false}) {
    if (loadMore) {
      pageNo++;
    } else {
      pageNo = 1;
    }
    try {
      http.netFetch<UserFriendListModel>(
        UserApi.getFriendList,
        method: Method.post,
        params: {
          'pageNo': pageNo,
          'pageSize': pageSize,
          'level': level.value,
          'showId': keyword.value,
        },
        success: (result) {
          List<UserFriendListItemModel> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNo--;
          }
          if (!loadMore) {
            friendList.clear();
          }
          friendList.addAll(items);
          total.value = result.data!.total!;
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNo--;
      }
    }
  }

  changeTab(String code) {
    if (code == level.value) {
      return;
    }
    level.value = code;
    getList();
  }

  getHotWords() async {
    try {
      return await http.netFetch<dynamic>(
        '${MallUrl.getHotWords}?count=20',
        method: Method.get,
        success: (result) {
          hotWordsList.value = result.result;
        },
      );
    } catch (e) {
      return Future.error(e);
    }
  }

  @override
  void onClose() {
    super.onClose();
    Get.delete<ScrollController>(); //删除控制器实例
    scrollController.dispose();
  }
}
